<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Books extends CI_Controller {

    function Books()
    {
        parent::__construct();

        // Check if the user is logged in
        if(! $this->session->userdata('user_id'))
        {
            // If not, redirect to the login page
            redirect(base_url() . 'login/');
        }
    }

    function index()
    {
        $this->load->helper('flexigrid');

        $this->load->model('user_model');
        $role = array('User', 'Author', 'Administrator');

        $result = $this->user_model->user_details(
                    $this->session->userdata('user_id'));

        if(! $result->num_rows() > 0)
        {
            redirect(base_url() . 'logout/');
        }

        $user = $result->row_array();
        $data['user']['name']   = $user['last_name'] . ', ' . 
                                    $user['first_name'];
        $data['user']['role']   = $role[$user['role']];

        // Set the columns and column properties
        $columns['fields'] = array(
            'id'        => array('Accession No.',   70,     TRUE,   'left', 2),
            'date'      => array('Date',            50,     TRUE,   'left', 1),
            'class'     => array('Class',           30,     FALSE,  'left', 1),
            'author'    => array('Author',          100,    TRUE,   'left', 1),
            'title'     => array('Title',           200,    TRUE,   'left', 1),
            'edition'   => array('Edition',         30,     TRUE,   'left', 0),
            'volumes'   => array('Volumes',         40,     FALSE,  'left', 0),
            'pages'     => array('Pages',           30,     FALSE,  'left', 0),
            'source'    => array('Source',          50,     TRUE,   'left', 1),
            'cost'      => array('Cost',            50,     TRUE,   'left', 1),
            'publisher' => array('Publisher',       100,    TRUE,   'left', 1),
            'year'      => array('Year',            30,     TRUE,   'left', 1),
            'remarks'   => array('Remarks',         50,     TRUE,   'left', 0)
        );
        $columns['sort'] = 'id';

        // Set the flexigrid properties
        $settings = array(
            'width'                 => 'auto',
            'height'                => 400,
            'rp'                    => 50,
            'rpOptions'             => '[10, 20, 30, 40, 50, 100, 150, 200]',
            'pagestat'              => 'Displaying: {from} to {to} of 
                                        {total} items.',
            'blockOpacity'          => 0.5,
            'showTableToggleBtn'    => FALSE,
            'singleSelect'          => 0,
            'title'                 => 'Book List'
        );

        $buttons[] = array('Add',           'add',      'grid');
        $buttons[] = array('separator');
        $buttons[] = array('Select All',    'select',   'grid');
        $buttons[] = array('De-select All', 'deselect', 'grid');
        $buttons[] = array('separator');
        $buttons[] = array('Edit',          'edit',     'grid');
        $buttons[] = array('Delete',        'delete',   'grid');
        $buttons[] = array('separator');
        $buttons[] = array('Import',        'import',   'grid');
        $buttons[] = array('Export',        'export',   'grid');
        $buttons[] = array('separator');
        $buttons[] = array('Print',         'print',    'grid');

        // Build flexigrid js
        $data['js_grid'] = build_grid_js(
                            'flexigrid', 
                            base_url() . 'ajax_books/grid', 
                            $columns['fields'], 
                            $columns['sort'], 
                            'asc', 
                            $settings, 
                            $buttons);

        $data['flexigrid_js']   = 1;
        $data['title']          = 'Books';
        $data['js']             = array('books_js');

        $this->load->view('header_view', $data);

        $this->load->view('user_view');
        $this->load->view('book_list_view');

        $this->load->view('footer_view');
    }

}

/* End of file books.php */
/* Location: ./application/controllers/books.php */